﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management;
using System.Threading;
using MySqlCodeGeneratorLib;

namespace DBCodeGenerator
{

    public partial class frmcsharpsettings : Form
    {
       
        CSharpGenerator csharp = null;
        List<CustomStoredProcedure> allprocs;
        MySqlInformation mysqlinfor;
        Thread tbind, tGenerate;
        int stdCount = 0;

        public frmcsharpsettings()
        {
            InitializeComponent();

            if (GlobalSettings.SelectedDatabaseType == dbtype.MSSQL_SERVER_2008R2)
            {
                csharp = new CSharpGenerator();
                csharp.StubComplete += new OnStubComplete(csharp_StubComplete);
                tbind = new Thread(new ThreadStart(BindProc));
                tGenerate = new Thread(new ThreadStart(Func_Generate));
            }
            else if (GlobalSettings.SelectedDatabaseType == dbtype.MYSQL_SERVER)
            {
                mysqlinfor = new MySqlInformation();
            }
            
            
            
            CheckForIllegalCrossThreadCalls = false;
        }

        void csharp_StubComplete(string procname)
        {
            prgbar.Increment(1);
            stdCount++;
            lblprgmsg.Text = prgbar.Maximum.ToString() + " of " + stdCount.ToString();
        }

        private void BindProc()
        {
            csharp.SelectedDatabase = cmballdb.SelectedItem.ToString();
            csharp.GetStoredProc();
            allprocs = csharp.StordProcData;
            gvstroedproc.DataSource = allprocs;
            prgbar.Maximum = allprocs.Count;
            lblmsglst.Text = "Found " + allprocs.Count.ToString();

        }

        private void btnsav_Click(object sender, EventArgs e)
        {
            if (cmballdb.Items.Count == 0)
            {
                MessageBox.Show("You need to login to the database server!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (String.IsNullOrEmpty(txtclassname.Text))
            {
                MessageBox.Show("Class name can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (String.IsNullOrEmpty(txtclsprefix.Text))
            {
                MessageBox.Show("Class prefix can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (String.IsNullOrEmpty(txtnamespace.Text))
            {
                MessageBox.Show("Namespace can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            tGenerate.Start();

        }

        private void Func_Generate()
        {
            allprocs = gvstroedproc.DataSource as List<CustomStoredProcedure>;
            csharp.Prefix = txtclsprefix.Text;
            csharp.clNamespace = txtnamespace.Text;
            csharp.ClassName = txtclassname.Text;
            csharp.StordProcData = allprocs;
            csharp.SelectedDatabase = cmballdb.SelectedItem.ToString();
            csharp.ReadDatabases();
            prgbar.Value = 0;
            lblprgmsg.Text = "";
            MessageBox.Show("Successfully Generated!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void frmcsharpsettings_Load(object sender, EventArgs e)
        {
            if (GlobalSettings.SelectedDatabaseType == dbtype.MSSQL_SERVER_2008R2)
                cmballdb.DataSource = csharp.GetAllDbs();
            else if (GlobalSettings.SelectedDatabaseType == dbtype.MYSQL_SERVER)
                cmballdb.DataSource = mysqlinfor.Login(GlobalSettings.ServerName, GlobalSettings.Uname, GlobalSettings.Password, GlobalSettings.Port);
        }

        private void btngetlst_Click(object sender, EventArgs e)
        {
            lblmsglst.Text = "Looking for stored procedures in " + cmballdb.SelectedItem.ToString();
            tbind = new Thread(new ThreadStart(BindProc));
            tbind.Start();
        }

        void ctl_Enter(object sender, EventArgs e)
        {
            (sender as ComboBox).DroppedDown = true;
        }

        private void gvstroedproc_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ComboBox ctl = gvstroedproc.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as ComboBox;
            if (ctl != null)
                ctl.DroppedDown = true;
        }

        private void gvstroedproc_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            DataGridViewComboBoxCell ctl = gvstroedproc.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell;

        }
    }
}
